package com.visionblue.business.ship.page.dialog

import android.app.Activity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.kennyc.view.MultiStateView
import com.visionblue.business.ship.R
import com.visionblue.business.ship.model.bean.response.ApplicationDetail
import com.visionblue.business.ship.model.enums.ApplicationRecordStatusEnum
import com.visionblue.business.ship.util.ext.setVisible
import com.visionblue.business.ship.util.toast

/**
 * @author: xlh
 * @date: 2020-06-08 3:36 PM
 * @desc:
 */
class TransferOutDialog(context: Activity) {

    private val mDialog: BlurrySlideDialog = BlurrySlideDialog(
        context,
        R.layout.dialog_transfer_out
    )

    fun showContent(
        detail: ApplicationDetail,
        hasNext: Boolean,
        hasPre: Boolean,
        onViewsClickListener: OnViewsClickListener? = null
    ) {
        mDialog.findViewById<MultiStateView>(R.id.multiStateView).viewState =
            MultiStateView.ViewState.CONTENT

        mDialog.showText(R.id.tvTransferShip, "转出对象：" + (detail.shipName ?: ""))
        mDialog.showText(R.id.tvMyShipName, detail.shipInfo?.shipName ?: "")

        val recordStatusEnum = ApplicationRecordStatusEnum.getRecordStatusEnum(detail.status)

        mDialog.findViewById<TextView>(R.id.tvRecall).setOnClickListener(null)

        if (recordStatusEnum == ApplicationRecordStatusEnum.PENDING) {
            mDialog.showText(R.id.tvStatus, "待接单")
            mDialog.findViewById<TextView>(R.id.tvRecall).visibility = View.VISIBLE
            mDialog.findViewById<TextView>(R.id.tvRecall).setOnClickListener {
                onViewsClickListener?.onRecallClick(detail.id)
            }
        } else if (recordStatusEnum == ApplicationRecordStatusEnum.PROCESSING) {
            mDialog.showText(R.id.tvStatus, "进行中")
            mDialog.findViewById<TextView>(R.id.tvRecall).visibility = View.INVISIBLE
        }

        mDialog.setColor(
            R.id.tvStatus,
            if (recordStatusEnum?.isGreen() == true) R.color.green0D else R.color.yellowF5
        )

        mDialog.findViewById<TextView>(R.id.tvNext).run {
            setVisible(hasNext)
            setOnClickListener {
                onViewsClickListener?.onNextClick()
            }
        }

        mDialog.findViewById<TextView>(R.id.tvPre).run {
            setVisible(hasPre)
            setOnClickListener {
                onViewsClickListener?.onPreClick()
            }
        }

        mDialog.findViewById<ImageView>(R.id.ivClose).setOnClickListener {
            mDialog.dismiss()
        }

        mDialog.setOnDismissListener {
            onViewsClickListener?.onDismiss()
        }

        mDialog.findViewById<View>(R.id.multiStateView).setOnClickListener {
            if (recordStatusEnum == ApplicationRecordStatusEnum.PROCESSING) {
                onViewsClickListener?.onItemClick(detail)
                mDialog.dismiss()
            } else {
                toast { "转运船还未同意，无法进行输出记录" }
            }
        }

        mDialog.show()
    }

    fun showEmpty(onViewsClickListener: OnViewsClickListener? = null) {

        mDialog.findViewById<MultiStateView>(R.id.multiStateView).viewState =
            MultiStateView.ViewState.EMPTY

        mDialog.findViewById<ImageView>(R.id.ivClose).setOnClickListener {
            mDialog.dismiss()
        }

        mDialog.findViewById<TextView>(R.id.tvStart).setOnClickListener {
            mDialog.dismiss()
            onViewsClickListener?.onStartClick()
        }

        mDialog.findViewById<View>(R.id.multiStateView).setOnClickListener(null)
        mDialog.show()
    }

    fun showLoading() {
        mDialog.findViewById<MultiStateView>(R.id.multiStateView).viewState =
            MultiStateView.ViewState.LOADING

        mDialog.findViewById<ImageView>(R.id.ivClose).setOnClickListener {
            mDialog.dismiss()
        }

        mDialog.findViewById<TextView>(R.id.tvStart).setOnClickListener {

        }

        mDialog.findViewById<View>(R.id.multiStateView).setOnClickListener(null)

        mDialog.show()
    }

    fun showError() {
        mDialog.findViewById<MultiStateView>(R.id.multiStateView).viewState =
            MultiStateView.ViewState.ERROR

        mDialog.findViewById<ImageView>(R.id.ivClose).setOnClickListener {
            mDialog.dismiss()
        }

        mDialog.findViewById<View>(R.id.multiStateView).setOnClickListener(null)

        mDialog.show()
    }


    interface OnViewsClickListener {

        /**
         * 委婉拒绝点击
         */
        fun onRecallClick(id: String?)

        /**
         * 整项被点击
         */
        fun onItemClick(detail: ApplicationDetail)

        /**
         * 上一个
         */
        fun onPreClick()

        /**
         * 下一个
         */
        fun onNextClick()

        /**
         * 对话框消失
         */
        fun onDismiss()

        fun onStartClick()
    }
}